package books;

import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;

import users.Person;
import util.Entity;
import builders.LoanBuilder;

/**
 * Value object denoting a Loan (good naming convention :D)
 */
public class Loan extends Entity {

    private static final long serialVersionUID = 1L;

    // ---------INSTANCE VARIABLES-----------------------------------------//

    private Person loaner;

    private Book loanedBook;

    private DateTime start;

    private DateTime end;

    private boolean finished;

    // ---------CONSTRUCTORS------------------------------------------------//

    public Loan() {
        super();
        // empty constructor
        start = new DateTime(DateTimeZone.forID("America/Argentina/Buenos_Aires"));
        finished = false;
    }

    // GOTTA WATCH IT FOR CONFIGURATION FILES
    public Loan(final DateTime anEnd, final Person aLoaner, final Book aBook) {
        super();
        loaner = aLoaner;
        loanedBook = aBook;
        start = new DateTime(DateTimeZone.forID("America/Argentina/Buenos_Aires"));
        end = anEnd;
        finished = false;
    }

    // ---------ACCESSORS---------------------------------------------------//

    public Loan(final LoanBuilder builder) {
        super();
        loanedBook = builder.getLoanedBook();
        loaner = builder.getLoaner();
        end = builder.getEnd();
        start = builder.getStart();
        finished = builder.isFinished();

    }

    public Person getLoaner() {
        return loaner;
    }

    public void setLoaner(final Person loaner) {
        this.loaner = loaner;
    }

    public Book getLoanedBook() {
        return loanedBook;
    }

    public void setLoanedBook(final Book loanedBook) {
        this.loanedBook = loanedBook;
    }

    public DateTime getStart() {
        return start;
    }

    public void setStart(final DateTime start) {
        this.start = start;
    }

    public DateTime getEnd() {
        return end;
    }

    public void setEnd(final DateTime end) {
        this.end = end;
    }

    public void finalizeLoan() {
        setFinished(true);
    }

    public BookInfo getBookInfo() {
        return getLoanedBook().getInfo();
    }

    public void setFinished(final boolean finished) {
        this.finished = finished;
    }

    public boolean isFinished() {
        return finished;
    }

    // ---------OTHER BEHAVIOUR---------------------------------------------//

}
